package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.bo.CommissionHistoryBO;
import com.junzhiit.nengji.dao.CommissionHistoryDAO;
import com.junzhiit.nengji.model.CommissionHistory;

@Repository
public class CommissionHistoryDAOImpl extends AbstractBaseDAO<CommissionHistory> implements CommissionHistoryDAO {

	@Override
	public List<CommissionHistoryBO> countCommissionHistory(String userId, String startTime, String endTime,int pageNumber, int maxPageRow) {
		StringBuffer sql = new StringBuffer();
		sql.append("select id,commission_money,created_time from commission_history ");
		sql.append(" where income_user_id = ?");
		StatementParameter params = new StatementParameter();
		params.setString(userId);
		if(StringUtils.isNoneBlank(startTime)){
			sql.append(" and created_time >= ?");
			params.setDate(DateUtil.getDateStart(DateUtil.parseDate(startTime)));
		}
		
		if(StringUtils.isNoneBlank(endTime)){
			sql.append(" and created_time < ?");
			params.setDate(DateUtil.getDateEnd(DateUtil.parseDate(endTime)));
		}
		sql.append(" order by created_time");
		return super.queryForPagingList(sql.toString(), CommissionHistoryBO.class, params, pageNumber, maxPageRow);
	}

	@Override
	public int getCount(String userId, String startTime, String endTime) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from commission_history ");
		sql.append(" where income_user_id = ?");
		StatementParameter params = new StatementParameter();
		params.setString(userId);
		if(StringUtils.isNoneBlank(startTime)){
			sql.append(" and created_time >= ?");
			params.setDate(DateUtil.getDateStart(DateUtil.parseDate(startTime)));
		}
		
		if(StringUtils.isNoneBlank(endTime)){
			sql.append(" and created_time < ?");
			params.setDate(DateUtil.getDateEnd(DateUtil.parseDate(endTime)));
		}
		sql.append(" order by created_time");
		return super.queryForInt(sql.toString(),params);
	}


}
